package com.easy.order.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.OrderAddr;
import com.easy.common.service.IOrderAddrService;
import com.easy.order.mapper.OrderAddrMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class OrderAddrServiceImpl extends ServiceImpl<OrderAddrMapper, OrderAddr> implements IOrderAddrService {

    @Override
    public OrderAddr getOrderAddrById(int id) {
        return super.getOne(new QueryWrapper<OrderAddr>().eq("id", id));
    }

    @Override
    public List<OrderAddr> getOrderAddrsByUserId(int userId) {
        LambdaQueryWrapper<OrderAddr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderAddr::getUser_id, userId);
        queryWrapper.eq(OrderAddr::getIs_del, 0);
        return super.list(queryWrapper);
    }

    @Override
    public List<OrderAddr> getOrderAddrsBySeckillGoodsId(int seckillGoodsId) {
        LambdaQueryWrapper<OrderAddr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderAddr::getSeckill_goods_id, seckillGoodsId);
        queryWrapper.eq(OrderAddr::getIs_del, 0);
        return super.list(queryWrapper);
    }

    @Override
    public List<OrderAddr> getOrderAddrsByGoodsId(int goodsId) {
        LambdaQueryWrapper<OrderAddr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderAddr::getGoods_id, goodsId);
        queryWrapper.eq(OrderAddr::getIs_del, 0);
        return super.list(queryWrapper);
    }

    @Override
    public OrderAddr addOrderAddr(OrderAddr orderAddr) {
        // 确保删除状态默认为0(未删除)
        if (orderAddr.getIs_del() == null) {
            orderAddr.setIs_del(0);
        }
        return super.save(orderAddr) ? orderAddr : null;
    }

    @Override
    public int updateOrderAddr(OrderAddr orderAddr) {
        return super.updateById(orderAddr) ? 1 : 0;
    }

    @Override
    public int deleteOrderAddr(Integer id) {
        // 软删除，将is_del字段置为1
        return super.update().set("is_del", 1).eq("id", id).update() ? 1 : 0;
    }

    @Override
    public List<OrderAddr> getAll(OrderAddr orderAddr) {
        LambdaQueryWrapper<OrderAddr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderAddr::getIs_del, 0);
        return super.list(queryWrapper);
    }

    @Override
    public IPage<OrderAddr> getPage(OrderAddr orderAddr, IPage<OrderAddr> page) {
        LambdaQueryWrapper<OrderAddr> queryWrapper = new LambdaQueryWrapper<>();
        if (orderAddr.getId() != null){
            queryWrapper.eq(OrderAddr::getId, orderAddr.getId());
        }
        if (orderAddr.getUser_id() != null){
            queryWrapper.eq(OrderAddr::getUser_id, orderAddr.getUser_id());
        }
        if (orderAddr.getSeckill_goods_id() != null){
            queryWrapper.eq(OrderAddr::getSeckill_goods_id, orderAddr.getSeckill_goods_id());
        }
        if (orderAddr.getGoods_id() != null){
            queryWrapper.eq(OrderAddr::getGoods_id, orderAddr.getGoods_id());
        }
        queryWrapper.eq(OrderAddr::getIs_del, 0);
        return super.page(page, queryWrapper);
    }
}
